using System;
using Gtk;
using RedCorona.Net;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net.Sockets;
using System.Threading;
using Silmoon;
using Silmoon.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Utilities;

namespace Levi.Finance.Client
{
	public class ABC : Silmoon.MySilmoon.SilmoonProductGBCInternat, Silmoon.MySilmoon.IRunningAble
	{
		public MainWindow win;
		public ListWindow list;
		public Query query;
		public Dialog dialog;
		public Configure configure;
		public Thread tcpSocketThread;
		public NameValueCollection Parameters = SmString.AnalyzeNameValue (Environment.GetCommandLineArgs (), "=", "--");
		public ABC(){
			Application.Init ();			
			this.OnThreadException += new System.Threading.ThreadExceptionEventHandler (ABC_OnThreadException);			
		}
		
		void ABC_OnThreadException (object sender, ThreadExceptionEventArgs e)
		{
			if (configure.server_setting_debug == false)
				return;
			Console.ForegroundColor = ConsoleColor.Red;
			Console.WriteLine ("===============================================================================");
			Console.ForegroundColor = ConsoleColor.DarkRed;
			Console.WriteLine (e.Exception.ToString ());
			Console.ForegroundColor = ConsoleColor.Red;
			Console.WriteLine ("===============================================================================");
			Console.ResetColor ();
		}
		
		public void showDialog(string title,string msg){
			this.dialog = new Dialog(title,this.win, Gtk.DialogFlags.DestroyWithParent);
			this.dialog.Modal = true;
			this.dialog.AddButton("Close", ResponseType.Close);
			this.dialog.Response += new ResponseHandler(on_dialog_response);
			this.dialog.Run();
			this.dialog.Destroy();
		}

		void on_dialog_response(object obj, ResponseArgs args)
		{
			Console.WriteLine(args.ResponseId);
		}

		public void GetFinanceList(int fromDate = 0,int toDate = 0,int income = 0){
			Package pack = new Package();
			pack.opt = 2;
			//pack.username = username;
			//pack.password = password;			
			this.query.SendMessage(JsonConvert.SerializeObject(pack));
		}
		
		public bool Start ()
		{
			if (RunningState == Silmoon.MySilmoon.RunningState.Running)
				return false;
			RunningState = Silmoon.MySilmoon.RunningState.Running;
			this.win = new MainWindow (this);
			this.win.Show ();
			this.list = new ListWindow(this);
			this.list.Hide();
			this.query = new Query(this);
			this.configure = new Configure(this);
			this.query.StartA();
			//this.tcpSocketThread = Threads.ExecAsync (query.StartA(), this.onThreadException);
			Application.Run();

			return true;
		}

		public void StartA ()
		{
			Start ();
		}

		public bool Suspend ()
		{
			return false;
		}

		public bool Resume ()
		{
			return false;
		}

		public bool Stop ()
		{
			if (RunningState == Silmoon.MySilmoon.RunningState.Stopped)
				return false;
			RunningState = Silmoon.MySilmoon.RunningState.Stopped;
			//server.Dispose ();
			//mongo.Dispose ();
			//server = null;
			return true;
		}
	}
}

